﻿function get_NewType_Html(){
  var htmlstr='<table id="Grid_NewType" style="width:auto;height:auto"></table><div id="addNewTypediv" style="display:none"></div><div id="edtNewTypediv" style="display:none"></div>';
  return htmlstr;
}
function getadd_NewType_Html(){
  var htmlstr='<div id="AddDlg_NewType" title="添加记录" icon="icon-window" style="padding:5px;background:#fafafa">';
  htmlstr=htmlstr+'<div class="easyui-layout" fit="true">';
  htmlstr=htmlstr+'<div region="center" border="false" style="padding:10px;background:#fff;border:1px solid #ccc;">';
  htmlstr=htmlstr+'<table width="380" border="0" class="dtable" align="center" cellpadding="4" cellspacing="0">';
  htmlstr=htmlstr+'<tr><td> 新闻分类名 </td>';
  htmlstr=htmlstr+'<td><input type="text" name="add_TypeName" id="add_TypeName" /></td></tr>';
  htmlstr=htmlstr+'</table></div><div region="south" border="false" style="text-align:right;padding-top:4px;height:30px;line-height:30px;">';
  htmlstr=htmlstr+'<a id="addbt_NewType" href="javascript:void(0)" onclick="Add_NewType(0)"> 添加顶级分类 </a>';
  htmlstr=htmlstr+'<a id="addbt_NewType2" href="javascript:void(0)" onclick="Add_NewType(1)" style="margin-left:5px"> 添加次级分类 </a>';
  htmlstr=htmlstr+'</div></div>';
  return htmlstr;
}
function getedt_NewType_Html(){
  var htmlstr='<div id="EditDlg_NewType" title="修改记录" icon="icon-window" style="padding:5px;background:#fafafa">';
  htmlstr=htmlstr+'<div class="easyui-layout" fit="true">';
  htmlstr=htmlstr+'<div region="center" border="false" style="padding:10px;background:#fff;border:1px solid #ccc;">';
  htmlstr=htmlstr+'<table width="380" border="0" class="dtable" align="center" cellpadding="4" cellspacing="0">';
  htmlstr=htmlstr+'<tr><td> 上级标识 </td>';
  htmlstr=htmlstr+'<td><input type="text" name="add_ParentId" id="edt_ParentId" /></td></tr>';
  htmlstr=htmlstr+'<tr><td> 新闻分类名 </td>';
  htmlstr=htmlstr+'<td><input type="text" name="add_TypeName" id="edt_TypeName" /></td></tr>';
  htmlstr=htmlstr+'</table></div><div region="south" border="false" style="text-align:right;padding-top:4px;height:30px;line-height:30px;">';
  htmlstr=htmlstr+'<a icon="icon-ok" id="edtbt_NewType" href="javascript:void(0)" onclick="Edit_NewType()"> 修 改 </a>';
  htmlstr=htmlstr+'</div></div>';
  return htmlstr;
}
function Add_NewType_Tab(tabname){
  if(!$('#ttab').tabs('exists',tabname)){
	$('#ttab').tabs('add',{
		title:tabname,
		cache:false,
		closable:true,
		content:get_NewType_Html()
	  });
	var gridht = $('body').layout('panel','center').panel('options').height - 58;
	$('#Grid_NewType').datagrid({
	  border:false,
	  nowrap: false,
	  striped: true,
	  collapsible:true,
	  url:'/Data/NewType.ashx',
	  method:'get',
	  height:gridht,
	  queryParams:{type:"0"},
	  fitColumns:true,
	  loadMsg:'正在读取数据,请稍候...',
	  singleSelect:true,
	  columns:[[
		{field:'NewTypeId',checkbox:true},
		{field:'ParentId',title:'上级标识',width:80},
		{field:'TypeName',title:'分类名',width:80}]],
	  pagination:true,
	  rownumbers:true,
	  toolbar:[{
		id:'btn_NewType_add',
		text:'添加分类',
		iconCls:'icon-add',
		handler:function(){
		  ShowNewTypeAddFrom();}
		},'-',{
		id:'btn_NewType_edit',
		text:'修改分类',
		iconCls:'icon-edit',
		handler:function(){
		  GetOne_NewType();}
		},'-',{
		id:'btn_NewType_del',
		text:'删除分类',
		iconCls:'icon-remove',
		handler:function(){
		 Del_NewType();}
	  }],
	  onLoadError:function(er){
		  $.messager.alert('错误','可能无查看权限,无法加载数据!','error');
	  }
	});
  }else{
	$('#ttab').tabs('select',tabname);
  }
}
function ShowNewTypeAddFrom(){
  $('#addNewTypediv').html(getadd_NewType_Html());
  $.parser.parse($('#addNewTypediv'));
  $('#AddDlg_NewType').window({
	  title: '添加新闻分类',
	  width: 450,
	  modal: true,
	  shadow: true,
	  closed: true,
	  height: 150
  });
  $('#addbt_NewType').linkbutton({iconCls:"icon-ok"});
  $('#addbt_NewType2').linkbutton({iconCls:"icon-ok"});
  $('#AddDlg_NewType').window('open');
}
function ShowNewTypeEdtFrom(){
  $('#edtNewTypediv').html(getedt_NewType_Html());
  $.parser.parse($('#edtNewTypediv'));
}
function Add_NewType(pid){
	if(pid>0){
	    var selected = $('#Grid_NewType').datagrid('getSelected');
		if(!selected){
	        $.messager.show({
				title:'提示',
				msg:'没有选择任何记录.',
				timeout:3000,
				showType:'slide'
			});
			return;
		}else{
			pid=selected.NewTypeId;
		}
	}
  $.get("/Data/NewType.ashx",{type:2,ParentId:pid,TypeName:$('#add_TypeName').val()},
  function(data){
    $('#AddDlg_NewType').window('close');
	$('#Grid_NewType').datagrid("reload");
  });
}
function GetOne_NewType(){
  var getid=0;
  var selected = $('#Grid_NewType').datagrid('getSelected');
  if (selected){
    getid=selected.NewTypeId;
	$.getJSON("/Data/NewType.ashx",{type:1,id:getid},
	function(data){
			ShowNewTypeEdtFrom();
			$('#EditDlg_NewType').window({
				title: '修改新闻分类',
				width: 450,
				modal: true,
				shadow: true,
				closed: true,
				height: 170
				});
			$('#EditDlg_NewType').window('open');
			$('#edtbt_NewType').linkbutton({iconCls:"icon-ok"});
						$('#edt_NewTypeId').val(data.model.NewTypeId);
						$('#edt_ParentId').val(data.model.ParentId);
						$('#edt_TypeName').val(data.model.TypeName);
			
      });
	}else{
	        $.messager.show({
				title:'提示',
				msg:'没有选择任何记录.',
				timeout:3000,
				showType:'slide'
			});
	}
}
function Edit_NewType(){
  var selected = $('#Grid_NewType').datagrid('getSelected');
  if(!selected){
	  $.messager.show({
		  title:'提示',
		  msg:'没有选择任何记录.',
		  timeout:3000,
		  showType:'slide'
	  });
	  return;
  }
  $.get("/Data/NewType.ashx",{type:4,NewTypeId:selected.NewTypeId,ParentId:$('#edt_ParentId').val(),TypeName:$('#edt_TypeName').val()},
  function(data){
    $('#EditDlg_NewType').window('close');
	$('#Grid_NewType').datagrid("reload");
  }); 
}
function Del_NewType(){
  var getid=0;
  var selected = $('#Grid_NewType').datagrid('getSelected');
  if(!selected){
	        $.messager.show({
				title:'提示',
				msg:'没有选择任何记录.',
				timeout:3000,
				showType:'slide'
			});
			return;
  }
  
  getid=selected.NewTypeId;
  $.messager.confirm('确认删除', '请确认是否进行此操作?', function(r){
	  if(r){
			$.get("/Data/NewType.ashx",{type:3,id:getid},
			function(data){
				$.messager.show({
					title:'提示',
					msg:'已删除数据.',
					timeout:3000,
					showType:'slide'
				});
				$('#Grid_NewType').datagrid("reload");
			}); 
		  }
  }); 
} 
  